<?php
/*
  PHP version 5
  Copyright (c) 2002-2010 ECISP.CN
  声明：这不是一个免费的软件，请在许可范围内使用

  作者：Bili E-mail:huangqyun@163.com  QQ:6326420
  http://www.ecisp.cn	http://www.easysitepm.com
 */

class mainpage extends connector {

	function mainpage() {
		$this->softbase(false);
		$this->mlink = $this->memberlink(array(), admin_LNG);
	}

	function in_center() {
		
		parent::start_pagetemplate();
		parent::member_purview();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$db_where = "userid=$this->ec_member_username_id AND username='$this->ec_member_username' ";
		$db_table1 = db_prefix . 'member AS a';
		$db_table2 = db_prefix . 'member_value AS b';
		$db_sql = "SELECT a.*,b.mvid,b.value FROM $db_table1 LEFT JOIN $db_table2 ON a.userid = b.userid  WHERE a.userid = $this->ec_member_username_id ";
		$rsMember = $this->db->fetch_first($db_sql);
		$rsMember['rankname'] = $this->get_member_purview($rsMember['mcid'], 'rankname');
		$userid = $rsMember['userid'];
		if (empty($userid)) {
			exit('user err!');
		}
		$db_table = db_prefix . "hotel_order";

		$db_where = " WHERE userid=$userid";

		$db_where2 = " WHERE userid=$userid and ordertype=1";

		$db_where3 = " WHERE userid=$userid and ordertype=3";
		$this->pagetemplate->assign('ordernum', $this->db_numrows($db_table, $db_where));
		//$this->pagetemplate->assign('ordernum2', $this->db_numrows($db_table, $db_where2));
		//$this->pagetemplate->assign('ordernum3', $this->db_numrows($db_table, $db_where3));

		$db_table = db_prefix . "bbs";
		$db_where = " WHERE userid=$userid";

		$this->pagetemplate->assign('messagenum', $this->db_numrows($db_table, $db_where));

		$templatesDIR = $this->get_templatesdir('member');

		$templatefilename = $lng . '/' . $templatesDIR . '/member_center';
		$this->pagetemplate->assign('out', 'center');
		$this->pagetemplate->assign('mlink', $this->mlink);
		$this->pagetemplate->assign('member', $rsMember);
		$this->pagetemplate->assign('path', 'member');
		unset($rsMember, $mlink, $LANPACK, $this->lng);
		$this->pagetemplate->display($templatefilename, 'center', false, null, admin_LNG);
	}

	function in_editinfo() {
		parent::start_pagetemplate();
		parent::member_purview();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$db_table1 = db_prefix . 'member AS a';
		$db_table2 = db_prefix . 'member_value AS b';
		$db_sql = "SELECT a.*,b.mvid,b.value FROM $db_table1
			   LEFT JOIN $db_table2 ON a.userid = b.userid
			   WHERE a.userid = $this->ec_member_username_id AND a.username='$this->ec_member_username' ";
		$rsMember = $this->db->fetch_first($db_sql);

		$attrread = $this->get_memberatt(false, true);
		if (!empty($rsMember['value'])) {
			$value = unserialize($rsMember['value']);
			unset($rsMember['value']);
		}
		if (is_array($value)) {
			$attlist = $this->fun->array_merge($attrread, $value);
		} else {
			$attlist = $attrread;
		}
		$templatesDIR = $this->get_templatesdir('member');

		$templatefilename = $lng . '/' . $templatesDIR . '/member_center';
		$this->pagetemplate->assign('out', 'editinfo');
		$this->pagetemplate->assign('mlink', $this->mlink);
		$this->pagetemplate->assign('attrread', $attlist);
		$this->pagetemplate->assign('member', $rsMember);
		$this->pagetemplate->assign('upurl', $_SERVER['HTTP_REFERER']);
		$this->pagetemplate->assign('path', 'member');
		unset($rsMember, $mlink, $LANPACK, $this->lng);
		$this->pagetemplate->display($templatefilename, 'center', false, null, admin_LNG);
	}

	function in_editpassword() {
		parent::start_pagetemplate();
		parent::member_purview();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$db_table1 = db_prefix . 'member AS a';
		$db_table2 = db_prefix . 'member_value AS b';
		$db_sql = "SELECT a.*,b.mvid,b.value FROM $db_table1
			   LEFT JOIN $db_table2 ON a.userid = b.userid
			   WHERE a.userid = $this->ec_member_username_id AND a.username='$this->ec_member_username' ";
		$rsMember = $this->db->fetch_first($db_sql);
		$templatesDIR = $this->get_templatesdir('member');

		$templatefilename = $lng . '/' . $templatesDIR . '/member_center';
		$this->pagetemplate->assign('out', 'editpassword');
		$this->pagetemplate->assign('mlink', $this->mlink);
		$this->pagetemplate->assign('attrread', $attlist);
		$this->pagetemplate->assign('member', $rsMember);
		$this->pagetemplate->assign('upurl', $_SERVER['HTTP_REFERER']);
		$this->pagetemplate->assign('path', 'member');
		unset($rsMember, $mlink, $LANPACK, $this->lng);
		$this->pagetemplate->display($templatefilename, 'center', false, null, admin_LNG);
	}

	function in_editemail() {
		parent::start_pagetemplate();
		parent::member_purview();
		$lng = (admin_LNG == 'big5') ? $this->CON['is_lancode'] : admin_LNG;
		$db_table1 = db_prefix . 'member AS a';
		$db_table2 = db_prefix . 'member_value AS b';
		$db_sql = "SELECT a.*,b.mvid,b.value FROM $db_table1
			   LEFT JOIN $db_table2 ON a.userid = b.userid
			   WHERE a.userid = $this->ec_member_username_id AND a.username='$this->ec_member_username' ";
		$rsMember = $this->db->fetch_first($db_sql);
		$templatesDIR = $this->get_templatesdir('member');

		$templatefilename = $lng . '/' . $templatesDIR . '/member_center';
		$this->pagetemplate->assign('out', 'editemail');
		$this->pagetemplate->assign('mlink', $this->mlink);
		$this->pagetemplate->assign('attrread', $attlist);
		$this->pagetemplate->assign('member', $rsMember);
		$this->pagetemplate->assign('upurl', $_SERVER['HTTP_REFERER']);
		$this->pagetemplate->assign('path', 'member');
		unset($rsMember, $mlink, $LANPACK, $this->lng);
		$this->pagetemplate->display($templatefilename, 'center', false, null, admin_LNG);
	}

	function in_save() {
		parent::start_pagetemplate();
		parent::member_purview();
		$inputclass = $this->fun->accept('inputclass', 'R');
		$upurl = $this->fun->accept('upurl', 'R');
		$userid = intval($this->fun->accept('userid', 'P'));
		$username = $this->fun->accept('username', 'P');
		if (empty($userid) || empty($username)) {
			$linkURL = $this->mlink['center'];
			$this->callmessage($this->lng['member_edit_ok'], $linkURL, $this->lng['gobackurlbotton']);
		}
		$email = trim($this->fun->accept('email', 'P'));
		$question = trim($this->fun->accept('question', 'P', true, true));
		$answer = trim($this->fun->accept('answer', 'P', true, true));
		$alias = trim($this->fun->accept('alias', 'P', true, true));
		$sex = $this->fun->accept('sex', 'P');
		$tel = trim($this->fun->accept('tel', 'P', true, true));
		$mobile = trim($this->fun->accept('mobile', 'P', true, true));
		$birthday = $this->fun->accept('birthday', 'P');
		$birthday = empty($birthday) ? 0 : $this->fun->formatdate($birthday, 4);
		$country = intval($this->fun->accept('cityone', 'P'));
		$province = intval($this->fun->accept('citytwo', 'P'));
		$city = intval($this->fun->accept('citythree', 'P'));
		$district = intval($this->fun->accept('district', 'P'));
		$address = trim($this->fun->accept('address', 'P', true, true));
		$zipcode = trim($this->fun->accept('zipcode', 'P', true, true));
		$zipcode = empty($zipcode) ? 0 : $zipcode;
		$msn = trim($this->fun->accept('msn', 'P', true, true));
		$qq = $this->fun->accept('qq', 'P');
		$qq = empty($qq) ? 0 : $qq;
		$memberatt = $this->fun->accept('memberatt', 'P');
		$db_table = db_prefix . 'member';
		$db_table2 = db_prefix . 'member_value';
		$date = time();
		$linkURL = $_SERVER['HTTP_REFERER'];

		if ($inputclass == 'editinfo') {
			$db_where = 'userid=' . $userid;
			$db_set = "sex=$sex,birthday=$birthday,country=$country,province=$province,city=$city,district=$district,alias='$alias',
			address='$address',zipcode=$zipcode,tel='$tel',mobile='$mobile',qq=$qq,msn='$msn'";
			$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);

			if (!empty($memberatt) && is_array($memberatt)) {

				$value = addslashes(serialize($memberatt));
				if ($this->checkdid($userid, 'member_value')) {
					$db_set = "value='$value'";
					$this->db->query('UPDATE ' . $db_table2 . ' SET ' . $db_set . ' WHERE ' . $db_where);
				} else {
					$db_field = 'userid,value';
					$db_values = "$userid,'$value'";
					$this->db->query('INSERT INTO ' . $db_table2 . ' (' . $db_field . ') VALUES (' . $db_values . ')');
				}
			}
			$linkURL = $this->mlink['center'];
			$this->callmessage($this->lng['member_edit_ok'], $linkURL, $this->lng['gobackurlbotton']);
		}

		if ($inputclass == 'editpassword') {
			
			$oldpassword = md5($this->fun->accept('oldpassword', 'P'));
			$password = md5($this->fun->accept('password', 'P'));
			$password_uc = $this->fun->accept('password', 'P');
			$oldpassword_uc = $this->fun->accept('oldpassword', 'P');

			$db_where = "userid= $userid AND username='$username' AND password='$oldpassword'";
			$db_sql = "SELECT * FROM $db_table WHERE $db_where";
			$rsMember = $this->db->fetch_first($db_sql);
			if (!$rsMember) {
				exit;
				$linkURL = $this->mlink['memedit_password'];
				$this->callmessage($this->lng['password_input_err'], $linkURL, $this->lng['gobackbotton']);
			} else {
				$db_set = "password='$password'";
				$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
				
				$linkURL = $this->mlink['quit'];
				$this->callmessage($this->lng['password_ok'], $linkURL, $this->lng['out_botton']);
			}
		}

		if ($inputclass == 'editmail') {
			
			$linkURL = $this->mlink['memedit_email'];

			if (!preg_match("/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i", $email)) {
				$this->callmessage($this->lng['email_err'], $linkURL, $this->lng['gobackbotton']);
			}
			$password = md5($this->fun->accept('password', 'P'));
			$password_uc = $this->fun->accept('password', 'P');
			$email = $this->fun->accept('email', 'P');
			$db_where = "userid= $userid AND username='$username' AND password='$password'";
			$db_sql = "SELECT * FROM $db_table WHERE $db_where";
			$rsMember = $this->db->fetch_first($db_sql);
			if (!$rsMember) {
				$this->callmessage($this->lng['password_input_err'], $linkURL, $this->lng['gobackbotton']);
			} else {
				$db_set = "email='$email'";
				$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
				
				$linkURL = $this->mlink['center'];
				$this->callmessage($this->lng['email_edit_ok'], $linkURL, $this->lng['gobackurlbotton']);
			}
		}
	}

	function checkdid($did, $dbname) {
		if (empty($did)) {
			return false;
		}
		$db_table = db_prefix . $dbname;
		$db_where = " WHERE userid='$did'";
		$countnum = $this->db_numrows($db_table, $db_where);
		if ($countnum > 0) {
			return true;
		} else {
			return false;
		}
	}

}
?>